﻿using PFC.CustomControls;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PFC.Program.Verification.Pages
{
    /// <summary>
    /// Логика взаимодействия для SearchDB.xaml
    /// </summary>
    public partial class ConnectDB : Page
    {
        /* Создание поля для совершения подключения к БД */
        SqlConnection conn = CommonClass.GetConnection();

        public ConnectDB()
        {
            InitializeComponent();

            /* Подключение к БД */
            try
            {
                conn.Open();

                if (conn.State.ToString() == "Open")
                {
                    infoField_Txb.Text = $"Подключение успешно";
                    criticalErrorWarning_Txb.Foreground = Brushes.SpringGreen;
                    criticalErrorWarning_Txb.Text = $" СОСТОЯНИЕ: {conn.State};\n\n КЛИЕНТ: {conn.WorkstationId};\n\n ЭКЗЕМПЛЯР: {conn.DataSource};\n\n ПУТЬ К БД: {conn.Database}";
                }

                refreshConnection_Btn.Visibility = Visibility.Hidden;
                connectionStatus_Img.Source = BitmapFrame.Create(new Uri(@"pack://application:,,,/Resources/Connection+.png"));

                loginBtn.IsEnabled = true;
                regisBtn.IsEnabled = true;

            } catch (SqlException ex) {
                criticalErrorWarning_Txb.VerticalAlignment = VerticalAlignment.Top;
                criticalErrorWarning_Txb.HorizontalAlignment = HorizontalAlignment.Center;
                criticalErrorWarning_Txb.Text = "ОШИБКА: \n" + ex.Message;

                infoField_Txb.Foreground = Brushes.Red;
                infoField_Txb.Text = "Проблемы с подключением! Обратитесь к админу";

                connectionStatus_Img.Source = BitmapFrame.Create(new Uri(@"pack://application:,,,/Resources/Connection-.png"));
                refreshConnection_Btn.Visibility = Visibility.Visible;
            }

        }

        /* Обновление соединения */
        private void refreshConnection_Btn_Click(object sender, RoutedEventArgs e)
        {
            try { conn.Open(); } catch (SqlException) { MessageBox.Show("Обратитесь к админу",
                                                        "Ошибка с подключением!", MessageBoxButton.OK,
                                                        MessageBoxImage.Information); };

            if (conn.State.ToString() == "Open")
            {
                infoField_Txb.Text = $"Подключение успешно к - '{conn.Database}'";
                infoField_Txb.Foreground = Brushes.SpringGreen;
                connectionStatus_Img.Source = BitmapFrame.Create(new Uri(@"pack://application:,,,/Resources/Connection+.png"));
                refreshConnection_Btn.Visibility = Visibility.Hidden;
                criticalErrorWarning_Txb.Text = "";

                loginBtn.IsEnabled = true;
                regisBtn.IsEnabled = true;
            }
        }

        /* Переход на страницу авторизации или регистрации */
        private void WrapPanel_Click(object sender, RoutedEventArgs e)
        {
            var ClickedButton = e.OriginalSource as NavButton;

            NavigationService.Navigate(ClickedButton.NavUri);
        }

        
    }

   
}

